package com.powernode.lambda.parallelStream;

import cn.hutool.core.io.FileUtil;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 读取data.txt文件
 * 1.先搭好读取框架，转换为Stream流
 * 2.取流的前5条数据(limit方法)作为调试使用，等最终结果没问题，再取全部数据
 * 3.统计分类方法groupingBy，进入源码可知：
 *         return groupingBy(classifier, HashMap::new, downstream);
 *   其中第二个参数不需要传参，直接new了一个HashMap，但hashMap没有排序
 *   因此直接增加第二个参数为TreeMap，既分组统计又排序
 */
public class parallel02 {
    public static void main(String[] args) {
        String prefix = System.getProperty("user.dir").replace("null", "");
        Path filePath = Paths.get(prefix+"\\ssm\\src\\main\\java\\com\\powernode\\lambda\\data.txt");
        System.out.println("尝试读取文件: " + filePath.toAbsolutePath());
        try (Stream<String> lines = Files.lines(filePath)) {
            // 逐行处理，这里简单打印每一行
            //lines.skip(1).limit(5).map(x-> Arrays.toString(x.split(","))).forEach(System.out::println);
            statisticUserSaleMax10(lines);
        } catch (IOException e) {
            System.err.println("读取文件失败: " + e.getMessage());
            e.printStackTrace();
        }

    }

    //统计每月销售量
    private static void statisticPerSales(Stream<String> lines) {
        Map<String, Long> collect = lines.skip(1)
                .map(x -> x.split(","))
                .collect(Collectors.groupingBy((x -> x[1].substring(0,7)), TreeMap::new,Collectors.counting()));
        for (Map.Entry<String, Long> entry : collect.entrySet()) {
            System.out.println(entry);
        }
    }
    //统计每月销售量中最高的
    private static void statisticPerSalesMax(Stream<String> lines) {
        Map<String, Long> collect = lines.skip(1)
                .map(x -> x.split(","))
                .collect(Collectors.groupingBy((x -> x[1].substring(0,7)), TreeMap::new,Collectors.counting()));
//        Optional<Map.Entry<String, Long>> max = collect.entrySet().stream().max(Comparator.comparingLong(x -> x.getValue()));
        Map.Entry<String, Long> max = collect.entrySet().stream().max(Map.Entry.comparingByValue()).get();
        System.out.println(max);
    }

    //统计销售量中最高的商品
    private static void statisticProductSalesMax(Stream<String> lines) {
        Map<String, Long> collect = lines.skip(1)
                .map(x -> x.split(","))
                .collect(Collectors.groupingBy((x -> x[3]), TreeMap::new,Collectors.counting()));
        Map.Entry<String, Long> max = collect.entrySet().stream().max(Map.Entry.comparingByValue()).get();
        System.out.println(max);

    }

    //统计下单最多的前十名用户
    private static void statisticUserSaleMax10(Stream<String> lines) {
        Map<String, Long> collect = lines.skip(1)
                .map(x -> x.split(","))
                .collect(Collectors.groupingBy((x -> x[8]), TreeMap::new,Collectors.counting()));
        List<Map.Entry<String, Long>> list = collect.entrySet().stream()
//                .sorted((a,b)->b.getValue().compareTo(a.getValue()))  //第一种逆序排序写法
                .sorted(Map.Entry.<String,Long>comparingByValue().reversed()) //必须要加泛型<String,Long>
                .limit(10).collect(Collectors.toList());

        for (Map.Entry<String, Long> stringLongEntry : list) {
            System.out.println(stringLongEntry.toString().replace("=", " 订单数"));
        }

    }


}
